package com.example.hospitaldemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitaldemo.entity.Nurse;
import com.example.hospitaldemo.service.INurseService;
import com.example.hospitaldemo.util.ResultData;
import com.github.promeg.pinyinhelper.Pinyin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 护士用户表 前端控制器
 * </p>
 *
 * @author 张三
 * @since 2024-12-16
 */
@RestController
@RequestMapping("/nurse")
@Tag(name = "登录控制器", description = "处理所有的登录功能!!!")
public class NurseController {
    @Autowired(required = false)
    private INurseService nurseService;
    /**
     * 登录功能
     *
     * 1.结合前端测试
     * 2.前端不在，需要单独的后台处理，测试
     *
     * @param nurse 用户信息
     * @return
     */
    @Operation(summary = "登录功能的处理方法.")
//    @ApiResponses({
//            @ApiResponse(responseCode = "200", description = "请求成功"),
//            @ApiResponse(responseCode = "400", description = "请求参数没填好"),
//            @ApiResponse(responseCode = "401", description = "没有权限"),
//            @ApiResponse(responseCode = "403", description = "禁止访问"),
//            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
//    })
    @PostMapping(value = "login",produces = "application/json")
    public ResultData login(@RequestBody Nurse nurse){
        Nurse one = nurseService.getOne(new QueryWrapper<>(new Nurse(nurse.getUsername(),nurse.getPassword())));
        if (one != null){
            return  ResultData.success(one);
        }
        return  ResultData.error(101,"用户名或密码错误！！！！！");
    }

    /**
     * 查询所有的护士/用户信息
     * 根据姓名检索用户
     *      分页的功能
     * @param page 页码
     * @param size  每页显示的条数
     * @param keyword 其他条件
     * @return
     */
    @GetMapping(value = "query")
    public ResultData queryALL(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(defaultValue = "") String keyword

    ){
        //分页所需的page对象
        Page page1 = new Page(page,size);
        //mybatis-plus的核心处理对象
        QueryWrapper queryWrapper = new QueryWrapper();
        //添加了查询条件
        queryWrapper.like("name",keyword);
        //分页查询
        nurseService.page(page1,queryWrapper);

        return ResultData.success(page1.getTotal(),page1.getRecords());

    }

    /**
     * 新增用户
     * @param nurse 只会传输真实姓名 name字段
     *              主键自增，
     *              密码设定固定值
     *              角色可以是固定值
     *              需要单独处理的是账号。
     *       真实姓名 张三 ==> zhangs
     *       真实姓名 王小明 ==> wangxm
     * @return
     */
    @PostMapping(value = "add")
    public ResultData add( @RequestBody Nurse nurse){

        String name = nurse.getName();//真实姓名
        StringBuffer username = new StringBuffer();
        //1.截取姓名中的第一个字符，字符转为拼音
        username.append(Pinyin.toPinyin(name.substring(0,1),"").toLowerCase());
        System.out.println(username);
        //2.后续的字符截取并转化为汉字的首字母
        for (int i=1;i<name.length();i++){
            username.append(Pinyin.toPinyin(name.substring(i,i+1),"").substring(0,1).toLowerCase());
        }
        //3.如果账户名相同，添加序号区分---查询数据库
        QueryWrapper<Nurse> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("username",username.toString());
        //4.判断是否重复
        long count = nurseService.count(queryWrapper) ;
        if (count > 0){
            username.append( count + 1);
        }
        //5.设定其他的字段值并保存到数据库
        nurse.setUsername(username.toString());
        nurse.setPassword("123456");
        nurse.setRole(2);
        if (nurseService.save(nurse)){
            return ResultData.success();
        }
        return ResultData.error(101,"新增失败！！！");
    }

    /**
     * 删除的方法
     * @return
     */
    @DeleteMapping(value = "delete")
    public ResultData delete(String id){
        if (nurseService.removeById(id)){
            return ResultData.success();
        }
        return  ResultData.error(101,"删除失败!!!!");
    }

    /**
     * 修改密码的方法
     * @return
     */
    @PostMapping(value = "saveOrUpdate")
    public ResultData updatePWD(@RequestBody Nurse nurse){
        if (nurseService.saveOrUpdate(nurse)){
            return ResultData.success();
        }
        return  ResultData.error(101,"修改密码失败!!!!");
    }
}
